import { _decorator, Component, Node,game,Color,color,instantiate,Sprite,tween,Vec3,v3,Quat, quat,UITransform} from 'cc';
import {GameMain} from "./GameMain";
import Util from "./Util";
import {AudioManager} from "./AudioManager";
import {BrickParticleManager} from "./BrickParticleManager";
const { ccclass, property } = _decorator;

@ccclass('BrickParticle')
export class BrickParticle extends Component
{
    public c:Color=color(255, 255, 255, 255);
    public targetGridIdx:number=0;

    private static readonly particleCnt:number=9;
    private static readonly blastCnt:number=10;

    onLoad()
    {

    }

    start() {

    }

    public play(c:Color)
    {
        let moveDir:Vec3=v3(1,0,0);
        let rot:Quat=quat(0,0,0,0);

        //console.log("** XXX targetGridIdx="+targetGridIdx);
        let targetWorldPos:Vec3=GameMain.instance.grids[targetGridIdx].node.getWorldPosition();
        for(let i=0;i<BrickParticle.particleCnt;i++)
        {
            let particle =BrickParticleManager.getParticle(); //instantiate(GameMain.instance.brickParticlePrefab);
            this.node.addChild(particle);
            particle.setPosition(0, 0, 0);
            let sprite: Sprite = particle.getComponent(Sprite);
            sprite.color = c;

            Quat.fromEuler(rot, 0, 0, Util.randRangeNumber(0, 359));

            moveDir.set(1, 0, 0);
            Vec3.transformQuat(moveDir, moveDir, rot);
            moveDir.normalize();

            tween(particle)
                .to(0.25, {position: v3(moveDir.x * Util.randRangeNumber(60, 80), moveDir.y * Util.randRangeNumber(60, 80), 0)})
                .call(() =>
                {
                })
                .start();
        }
    }

    public blast(c:Color)
    {
        let moveDir:Vec3=v3(1,0,0);
        let rot:Quat=quat(0,0,0,0);

        //console.log("** XXX targetGridIdx="+targetGridIdx);

        for(let i=0;i<BrickParticle.blastCnt;i++)
        {
            let particle =BrickParticleManager.getParticle(); //instantiate(GameMain.instance.brickParticlePrefab);
            this.node.addChild(particle);
            particle.setPosition(0, 0, 0);
            let sprite: Sprite = particle.getComponent(Sprite);
            sprite.color = c;

            Quat.fromEuler(rot, 0, 0, Util.randRangeNumber(0, 359));

            moveDir.set(1, 0, 0);
            Vec3.transformQuat(moveDir, moveDir, rot);
            moveDir.normalize();

            tween(particle)
                .to(0.3, {position: v3(moveDir.x * Util.randRangeNumber(100, 110), moveDir.y * Util.randRangeNumber(100, 110), 0)})
                .call(() =>
                {
                    particle.removeFromParent(true);
                    //particle.destroy();
                    BrickParticleManager.putParticle(particle);
                })
                .start();
        }
    }
}

